﻿using Bilgesoft.Framework.Server.DatabaseLayer;
using Bilgesoft.HRS.Server.DomainObjects;
using Bilgesoft.HRS.Shared.DataObjects;
using Bilgesoft.HRS.Shared.DataObjects.Criterias;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bilgesoft.HRS.Server.BusinessLayer
{
    public class HrsBusinessManagement
    {
        public HospitalDTO[] SearchHospital(SearchHospitalCriteria criteria)
        {
            using (DatabaseContext db = DatabaseContext.CreateContext("OHRSEntities1"))
            {
                DbSet<Hospital> hospitals = db.Set<Hospital>();
                DbSet<HospitalPolyclinic> hospitalpolyclinics = db.Set<HospitalPolyclinic>();
                DbSet<Polyclinic> polyclinics = db.Set<Polyclinic>();
                DbSet<Person> persons = db.Set<Person>();

                DbSet<Doctor> doctors = db.Set<Doctor>();
                DbSet<DoctorHospitalPolyclinic> doctorhospitalclinics = db.Set<DoctorHospitalPolyclinic>();
                DbSet<Town> towns = db.Set<Town>();
                DbSet<City> cities = db.Set<City>();


                criteria.TownIds = criteria.TownIds == null ? new int[0] : criteria.TownIds;
                var query = from h in hospitals
                            join t in towns on h.TownId equals t.Id
                            join ci in cities on t.CityId equals ci.Id
                            join hp1 in
                                (
                                    from hp in hospitalpolyclinics
                                    join c in polyclinics on hp.PolyclinicId equals c.Id
                                    join d1 in
                                        (
                                            from dh in doctorhospitalclinics
                                            where dh.EndDate == null
                                            join d in doctors on dh.DoctorId equals d.Id
                                            join p in persons on d.PersonId equals p.Id
                                            select new
                                            {
                                                doctorId = d.Id,
                                                p.Name,
                                                p.Surname,
                                                p.IdentityNumber,
                                                d.PersonId,
                                                dh.HospitalPolyclinicId
                                            }
                                        ) on hp.Id equals d1.HospitalPolyclinicId into dtors
                                    select new
                                    {
                                        hp.HospitalId,
                                        c.Id,
                                        clinicname = c.Name,
                                        doctors = dtors
                                    }
                                ) on h.Id equals hp1.HospitalId into clinics
                            where
                            (!criteria.NotFilterHospitalWithoutClinics || clinics.Count() > 0) &&
                            (criteria.TownIds.Count() == 0 || criteria.TownIds.Contains(h.TownId)) &&
                            (string.IsNullOrEmpty(criteria.HospitalName) || h.Name.Contains(criteria.HospitalName))
                            select new
                            {
                                h.Id,
                                h.Name,
                                townname = t.Name,
                                cityname = ci.name,
                                clinics
                            };

                var result = query.ToArray();
                List<HospitalDTO> _result = new List<HospitalDTO>();
                if (result != null && result.Length > 0)
                {
                    foreach (var item in result)
                    {
                        HospitalDTO h = new HospitalDTO();
                        h.CityName = item.cityname;
                        h.Name = item.Name;
                        h.TownName = item.townname;
                        if (item.clinics != null && item.clinics.Count() > 0)
                        {
                            List<PolyClinicDTO> clinicList = new List<PolyClinicDTO>();
                            foreach (var itemclinic in item.clinics)
                            {
                                PolyClinicDTO c = new PolyClinicDTO();
                                c.Name = itemclinic.clinicname;
                                
                                if (itemclinic.doctors != null && itemclinic.doctors.Count() > 0)
                                {
                                    List<DoctorDTO> doctorlist = new List<DoctorDTO>();
                                    foreach (var itemdoctor in itemclinic.doctors)
                                    {
                                        DoctorDTO d = new DoctorDTO();
                                        d.Id = itemdoctor.doctorId;
                                        d.IdentityNumber = itemdoctor.IdentityNumber;
                                        d.Name = itemdoctor.Name;
                                        d.Surname = itemdoctor.Surname;
                                        doctorlist.Add(d);
                                    }
                                    c.Doctors = doctorlist.ToArray();
                                }
                                clinicList.Add(c);
                            }
                            h.PolyClinics = clinicList.ToArray();
                        }
                        _result.Add(h);
                    }
                }
                return _result.ToArray();
            }
        }
    }
}
